package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.NAsphaltMatch;
import com.ruoyi.zhgd.domain.NAsphaltMatchThreshold;
import com.ruoyi.zhgd.domain.NAsphaltMatchType;
import com.ruoyi.zhgd.domain.vo.*;
import com.ruoyi.zhgd.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NAsphaltDataSieveBo;
import com.ruoyi.zhgd.domain.NAsphaltDataSieve;
import com.ruoyi.zhgd.mapper.NAsphaltDataSieveMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青拌合站数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NAsphaltDataSieveServiceImpl implements INAsphaltDataSieveService {

    private final NAsphaltDataSieveMapper baseMapper;

    @Autowired
    @Lazy
    private INAsphaltDataService inAsphaltDataService;

    @Autowired
    @Lazy
    private INAsphaltDataSieveService inAsphaltDataSieveService;

    @Autowired
    @Lazy
    private INAsphaltMatchService inAsphaltMatchService;

    @Autowired
    @Lazy
    private INAsphaltMatchTypeService inAsphaltMatchTypeService;

    @Autowired
    @Lazy
    private INAsphaltMatchThresholdService inAsphaltMatchThresholdService;

    /**
     * 查询沥青拌合站数据
     */
    @Override
    public NAsphaltDataSieveVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public NAsphaltDataSieveVo getById(String id) {
        return baseMapper.getById(id);
    }

    /**
     * 查询沥青拌合站数据列表
     */
    @Override
    public TableDataInfo<NAsphaltDataSieveVo> queryPageList(NAsphaltDataSieveBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NAsphaltDataSieve> lqw = buildQueryWrapper(bo);
        Page<NAsphaltDataSieveVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青拌合站数据列表
     */
    @Override
    public List<NAsphaltDataSieveVo> queryList(NAsphaltDataSieveBo bo) {
        LambdaQueryWrapper<NAsphaltDataSieve> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NAsphaltDataSieve> buildQueryWrapper(NAsphaltDataSieveBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NAsphaltDataSieve> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSieve0d075() != null, NAsphaltDataSieve::getSieve0d075, bo.getSieve0d075());
        lqw.eq(bo.getSieve0d15() != null, NAsphaltDataSieve::getSieve0d15, bo.getSieve0d15());
        lqw.eq(bo.getSieve0d3() != null, NAsphaltDataSieve::getSieve0d3, bo.getSieve0d3());
        lqw.eq(bo.getSieve0d6() != null, NAsphaltDataSieve::getSieve0d6, bo.getSieve0d6());
        lqw.eq(bo.getSieve1d18() != null, NAsphaltDataSieve::getSieve1d18, bo.getSieve1d18());
        lqw.eq(bo.getSieve2d36() != null, NAsphaltDataSieve::getSieve2d36, bo.getSieve2d36());
        lqw.eq(bo.getSieve4d75() != null, NAsphaltDataSieve::getSieve4d75, bo.getSieve4d75());
        lqw.eq(bo.getSieve9d5() != null, NAsphaltDataSieve::getSieve9d5, bo.getSieve9d5());
        lqw.eq(bo.getSieve13d2() != null, NAsphaltDataSieve::getSieve13d2, bo.getSieve13d2());
        lqw.eq(bo.getSieve16() != null, NAsphaltDataSieve::getSieve16, bo.getSieve16());
        lqw.eq(bo.getSieve19() != null, NAsphaltDataSieve::getSieve19, bo.getSieve19());
        lqw.eq(bo.getSieve26d5() != null, NAsphaltDataSieve::getSieve26d5, bo.getSieve26d5());
        lqw.eq(bo.getSieve31d5() != null, NAsphaltDataSieve::getSieve31d5, bo.getSieve31d5());
        lqw.eq(bo.getSieve37d5() != null, NAsphaltDataSieve::getSieve37d5, bo.getSieve37d5());
        lqw.eq(bo.getSieve53() != null, NAsphaltDataSieve::getSieve53, bo.getSieve53());
        lqw.eq(bo.getSieve52() != null, NAsphaltDataSieve::getSieve52, bo.getSieve52());
        return lqw;
    }

    /**
     * 新增沥青拌合站数据
     */
    @Override
    public Boolean insertByBo(NAsphaltDataSieveBo bo) {
        NAsphaltDataSieve add = BeanUtil.toBean(bo, NAsphaltDataSieve.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青拌合站数据
     */
    @Override
    public Boolean updateByBo(NAsphaltDataSieveBo bo) {
        NAsphaltDataSieve update = BeanUtil.toBean(bo, NAsphaltDataSieve.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NAsphaltDataSieve entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青拌合站数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 生产数据监控级配分页
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NAsphaltDataSieveVo> page(NAsphaltDataSieveBo bo, PageQuery pageQuery) {
        Page<NAsphaltDataSieveVo> result = baseMapper.page(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 获取全天筛孔数据
     *
     * @param stationCode 拌和站编号
     * @param matchCode   级配编号
     * @param day         日期
     * @return 全天筛孔数据
     */
    @Override
    public List<NAsphaltDataSieveVo> listForDay(String stationCode, String matchCode, String day) {
        List<NAsphaltDataSieve> list = baseMapper.listForDay(stationCode, matchCode, day);
        return BeanUtil.copyToList(list, NAsphaltDataSieveVo.class);
    }

    /**
     * 获取级配曲线图表数据
     *
     * @param id
     * @return
     */
    @Override
    public NAsphaltDataSieveOtherVo getChartData(String id) {
        // 获取生产数据
        NAsphaltDataVo data = inAsphaltDataService.getById(id);
        if (null == data) {
            throw new ServiceException("生产数据不存在");
        }
        // 需要返回的结果数据
        NAsphaltDataSieveOtherVo vo = new NAsphaltDataSieveOtherVo();
        vo.setData(data);
        // 获取筛分数据
        NAsphaltDataSieveVo sieveVo = inAsphaltDataSieveService.getById(id);
        vo.setSieveData(sieveVo);
        // 获取设计生产配置比
        NAsphaltMatch match = inAsphaltMatchService.getByCode(data.getMatchCode());
        vo.setMatch(BeanUtil.toBean(match, NAsphaltMatchVo.class));
        // 获取上下限
        NAsphaltMatchThreshold matchThreshold = inAsphaltMatchThresholdService.getByMatchName(data.getMatchName());
        vo.setMatchThreshold(BeanUtil.toBean(matchThreshold, NAsphaltMatchThresholdVo.class));
        // 获取级配类型
        NAsphaltMatchType matchType = inAsphaltMatchTypeService.getByType(data.getMatchType());
        vo.setAsphaltMatchType(BeanUtil.toBean(matchType, NAsphaltMatchTypeVo.class));
        // 返回结果数据
        return vo;
    }

    /**
     * 关键筛孔通过率
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NAsphaltProduceSievePassRateVo> produceSievePassRate(NAsphaltDataSieveBo bo, PageQuery pageQuery) {
        // 判断筛孔数据是否为空
        String sieveSize = ObjectUtils.isEmpty(bo.getSieveSize()) ? "0.075" : bo.getSieveSize();
        if ("2.36".equals(sieveSize)) {
            sieveSize = "sieve2d36";
        } else if ("4.75".equals(sieveSize)) {
            sieveSize = "sieve4d75";
        } else {
            sieveSize = "sieve0d075";
        }
        bo.setSieveSize(sieveSize);
        return TableDataInfo.build(baseMapper.produceSievePassRate(bo, pageQuery.build()));
    }

    /**
     * 获取全天筛孔平均值
     *
     * @param stationCode
     * @param matchCode
     * @param day
     * @return
     */
    @Override
    public NAsphaltDataSievingAvgVo avgForDay(String stationCode, String matchCode, String day) {
        return baseMapper.avgForDay(stationCode, matchCode, day);
    }

}
